<?php

namespace app\controllers;
use yii\data\ActiveDataProvider;
use Yii;
use app\models\CarUser;


class CarUserController extends \yii\rest\ActiveController
{
	public $modelClass = 'app\models\CarUser';


	public function behaviors()
    {
        return \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
            //响应返回json
            'contentNegotiator' => [
                'formats' => [
                    'application/json' => \yii\web\Response::FORMAT_JSON,
                ],
            ],
            /*//接口采用参数带token的方式进行认证 
            'authenticator' => [
            'class' => QueryParamAuth::className(),
            ],*/
    ]);
    }

	public $serializer = [
         'class' => 'yii\rest\Serializer',
         'collectionEnvelope' => 'items'
     ];


    

//添加用户接口

     public function actionTj()
     {

        if (!isset($_POST["tel"])||!isset($_POST["password"]))
         { throw new \yii\web\HttpException(400, '请填写正确的参数！');}
         $model = new $this->modelClass;
        try {
            $provider = new ActiveDataProvider([
                'query' => $model->find()->where(['tel'=>$_POST["tel"]]),
                'pagination' => false
            ]);
        } catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }
         if ($provider->getCount() != 0)
         {throw new \yii\web\HttpException(400, '手机号码已存在！');}
         // $model->load(Yii::$app->getRequest()
         // ->getBodyParams(), '');
         $model->attributes = Yii::$app->request->post();
         $model->save();
         if (! $model->save()) {
             return array_values($model->getFirstErrors())[0];
         }
        return $model;
     }

//用户登录接口
     public function actionDl()
     {

       if (!isset($_POST["tel"])||!isset($_POST["password"]))
         { throw new \yii\web\HttpException(400, '请填写正确的参数！');}

        try {
            $carusertel =CarUser::findOne(
    ['tel' => $_POST["tel"]]);
        } catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }

        if($carusertel===null)
    	 throw new \yii\web\HttpException(400, '手机号不存在！');
        
       try {
            $caruser =CarUser::findOne([
    'tel' => $_POST["tel"],
     'password' => $_POST["password"]
]);
        } catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }
      
    if($caruser===null)
    	 throw new \yii\web\HttpException(400, '用户名或密码错误！');
      return $caruser;
         
     }

//用户注销接口
 	 public function actionSc()
     {

     	  if (!isset($_POST["id"])||!isset($_POST["password"]))
         { throw new \yii\web\HttpException(400, '请填写正确的参数！');}
     	 try {
            $caruser =CarUser::findOne([
     'id' => $_POST["id"],
     'password' => $_POST["password"]
         ]);
        } catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }
      
    if($caruser===null)
    	 throw new \yii\web\HttpException(400, '对不起，密码错误，注销账户失败！');
    	else
    		$id=$_POST["id"];
        try{
         CarUser::findOne([
     'id' => $_POST["id"]])->delete();}
         catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }


     }

//修改密码接口
     public function actionXgmm()
     {

        if (!isset($_POST["id"])||!isset($_POST["oldpassword"])||!isset($_POST["newpassword"]))
         { throw new \yii\web\HttpException(400, '请填写正确的参数！');}
         try {
            $caruser =CarUser::findOne([
     'id' => $_POST["id"],
     'password' => $_POST["oldpassword"]
     ]);
        } catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }
      
    if($caruser===null)
         throw new \yii\web\HttpException(400, '对不起，原密码错误，密码修改失败！');
        else
           
            try {
            $caruser->password=$_POST["newpassword"];
            $caruser->save();
        } catch (Exception $ex) {
            throw new \yii\web\HttpException(500, 'Internal server error');
        }
          return $caruser;
     }


}
